<?php

/*
 * @description: Модель для работы с курсами
 * @copyright Copyright (c) 2012-2017 Marin V. , Tykva A. , Kozel R. , Sokolov D.
 * @version 1.0
 * @link http://lecturebox.ru
*/

class Model_Course
{
    protected $course_id;

    protected $_dbTable;
    protected $_row;
    
    protected $data;

    public function __construct($course_id=0)
    {
        $this->course_id = $course_id;
        $this->data = null;

        $this->_dbTable = new Model_DbTable_Courses();
        if($course_id)
            $this->_row = $this->_dbTable->find($course_id)->current();
        else
            $this->_row = $this->_dbTable->createRow();

    }

    public function fill($data)
    {
        foreach ($data as $key => $value)
            if (isset($this->_row->$key))
                $this->_row->$key = $value;
    }

    public function save()
    {
        $this->_row->save();
    }

    //Getters and Setters

    public function __set($name, $val)
    {
        if (isset($this->_row->$name)) {
            $this->_row->$name = $val;
        }
    }

    public function __get($name)
    {
        if (isset($this->_row->$name)) {
            return $this->_row->$name;
        }
    }

    public function getAllCourses()
    {
        return $this->_dbTable->fetchAll();
    }
    
    public function getCoursesByIdSub($id)
    {   
        $select = $this->_dbTable->select();
        $select->where('subject_id = ?', $id);
        return $this->_row = $this->_dbTable->fetchAll($select);
    }
    
    public function getCoursesByIdUser($id)
    {   
        $select = $this->_dbTable->select();
        $select->where('user_id = ?', $id);
        return $this->_row = $this->_dbTable->fetchAll($select);
    }
    
    public function getCoursesById($id)
    {   
        $select = $this->_dbTable->select();
        $select->where('course_id = ?', $id);
        return $this->_row = $this->_dbTable->fetchAll($select);
    }

    /**
     * Возвращает объект Zend_Paginator для вывода записей постранично
     * @param int $page
     * @param int $perPege
     * @return Zend_Paginator
     */
    public function getPaginatorRows($page = 1, $perPege = 10, $tag = 0, $cat = 0)
    {
        $adapter = new Model_Paginator_PageCourses(Zend_Db_Table::getDefaultAdapter()->select());
     
        $adapter->tag = $tag;
        $adapter->cat = $cat;

        // Создаем Zend_Paginator - передаем в него полученый адаптер
        $paginator = new Zend_Paginator($adapter);
        $paginator->setCurrentPageNumber($page);
        $paginator->setItemCountPerPage($perPege);

        return $paginator;
    }
    
//    public function findForSelect()
//    {
//    	$select = $this->select();
//    	return $this->fetchAll($select);
//    }
    
    public function populateForm()
    {
        return $this->_row->toArray();
    }

    /**
     * Получаем информацию из БД о данном курсе
     */
    public function getData()
    {
        if($this->data === null)
        {
            $this->data = Zend_Db_Table::getDefaultAdapter()->fetchRow(
                    'SELECT * FROM '
                        . TABLE_PREF . 'courses WHERE course_id=? LIMIT 1;',
                    array($this->course_id));
        }
        return $this->data;
    }
    
    public function delCourse($course_id)
    {   
        $where = array(
            'course_id = ?' => $course_id
        );        
        $this->_dbTable->delete($where); 
        $lecs = new Model_Lecture();
        $items = $lecs->getCourseLectures($course_id);
        foreach ($items as $item){
                $ranks = new Model_Rank();
                        $ran = $ranks->getRankByLectureId($item->lecture_id);
                        foreach ($ran as $rank){
                           $rank->delete();
                        }
                        $timeTables = new Model_Timeline();
                        $timeL = $timeTables->getTimelineByLecture($item->lecture_id);
                        foreach ($timeL as $timeTable){
                            $timeTable->delete();
                        }
        }
                        
        //удаляем все лекции курса
        $data = array(
            'writecode' => 'dub'
        );
        Zend_Db_Table::getDefaultAdapter()->update(
                     TABLE_PREF . 'lectures', $data, 'course_id=' . $course_id);
    }

}
